package com.lg.erp.controller.chenjunhui;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SCompany;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.chenjunhui.TOrderC;
import com.lg.erp.entity.chenjunhui.TProductHandover;
import com.lg.erp.entity.chenjunhui.TProductionLine;
import com.lg.erp.entity.chenkang.TWare;
import com.lg.erp.entity.yqy.TProduct;
import com.lg.erp.mapper.UserMapper;
import com.lg.erp.mapper.chenjunhui.TOrderMapper;
import com.lg.erp.service.ISCompanyService;
import com.lg.erp.service.chenjunhui.ITOrderService;
import com.lg.erp.service.chenjunhui.ITProductHandoverService;
import com.lg.erp.service.chenjunhui.ITProductionLineService;
import com.lg.erp.service.chenkang.IWareService;
import com.lg.erp.service.yqy.ITProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lg
 * @since 2021-04-21
 */
@RestController
public class TProductHandoverController {

    @Autowired
    private ITProductHandoverService itProductHandoverService;
    @Autowired
    private ITProductService itProductService;
    @Autowired
    private ITProductionLineService itProductionLineService;
    @Autowired
    private IWareService itWareService;
    @Autowired
    private ITOrderService itOrderService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ISCompanyService isCompanyService;
    @Autowired
    private TOrderMapper tOrderMapper;

    private List<TProductHandover> tProductHandovers = new ArrayList<>();

    private String time;


    /**
     * 查询所有商品
     * @return
     */
    @GetMapping("/selectProductsCJH")
    public JsonResult selectProducts(){
        List<TOrderC> tOrderCS = itOrderService.getTOrderByzt();
        System.out.println(tOrderCS);
        return new JsonResult(1,"ok",tOrderCS);
    }

    /**
     * 查询所有生产产线
     * @return
     */
    @GetMapping("/selectTProductionLinesCJH")
    public JsonResult selectTProductionLines(){
        List<TProductionLine> tProductionLines = itProductionLineService.getTProductionLines("0");
        System.out.println(tProductionLines);
        return new JsonResult(1,"ok",tProductionLines);
    }

    /**
     * 查询所有收货仓库
     * @return
     */
    @GetMapping("/selectTWaresCJH")
    public JsonResult selectTWares(){
        List<TWare> tWares = itWareService.list();
        System.out.println(tWares);
        return new JsonResult(1,"ok",tWares);
    }

    /**
     * 分页查询+模糊查询
     * @param pageNum
     * @param pageSize
     * @param formInlineId
     * @param formInlineDate
     * @return
     */
    @RequestMapping("/PageTProductHandover/{pageNum}/{pageSize}/{formInlineId}/{formInlineDate}")
    public JsonResult selectPage(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @PathVariable String formInlineId,
                                 @PathVariable String formInlineDate){
        //查询数据数量
        int i = itProductHandoverService.getTProductHandoverCount(formInlineId,formInlineDate);
        //分页查询数据
        List<TProductHandover> tProductHandovers=itProductHandoverService.getTProductHandoverPage(pageNum,pageSize,formInlineId,formInlineDate);
        for (TProductHandover tProductHandover : tProductHandovers) {
            TOrderC tOrder = tOrderMapper.selectTOrderH(tProductHandover.getOrderId());
            if (tOrder!=null){
                tProductHandover.setProductionName(tOrder.getProductionName());
            }
        }
        return new JsonResult(1,String.valueOf(i),tProductHandovers);
    }

    /**
     * 模糊查询
     * @param inp
     * @return
     */
    @ResponseBody
    @GetMapping("/searchTProductHandoverSelect/{inp}/{pageNum}/{pageSize}")
    public JsonResult selectUserRoles(@PathVariable String inp,
                                      @PathVariable Integer pageNum,
                                      @PathVariable Integer pageSize){
        //查询数据数量
        int i = itProductHandoverService.getAllCount(inp);
        //模糊查询
        List<TProductHandover> tProductHandovers = itProductHandoverService.getAllTProductHandoverPage(pageNum,pageSize,inp);
        for (TProductHandover tProductHandover : tProductHandovers) {
            TOrderC tOrder = tOrderMapper.selectTOrderH(tProductHandover.getOrderId());
            if (tOrder!=null){
                tProductHandover.setProductionName(tOrder.getProductionName());
            }
        }
        return new JsonResult(1,String.valueOf(i),tProductHandovers);
    }
    @GetMapping("/selectXuan/{id}")
    public JsonResult selectXuan(@PathVariable String id){
        List<TOrderC> tOrder = itOrderService.getTOrderById(id);
        return new JsonResult(1,"ok",tOrder);
    }

    /**
     * 添加交接单
     * @param tProductHandover
     * @return
     */
    @PostMapping("/addIProductHandover")
    public JsonResult addIProductHandover(@RequestBody TProductHandover tProductHandover){
        System.out.println(tProductHandover);
        List<TOrderC> tOrder = itOrderService.getTOrderById(tProductHandover.getOrderId());
        for (TOrderC tOrderC : tOrder) {
            tProductHandover.setOrderId(tOrderC.getOrderId());
        }
        //获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        SUser suser = userMapper.selectOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
        //获取公司信息
        SCompany sCompany = isCompanyService.getById(suser.getCompanyId());
        tProductHandover.setId(UUID.randomUUID().toString().substring(0,6).replace("-",""));
        tProductHandover.setType(0);
        tProductHandover.setState(0);
        tProductHandover.setCid(sCompany.getId());
        itProductHandoverService.save(tProductHandover);
        return new JsonResult(1,"ok",null);
    }

    /**
     * 删除交接单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTProductHandover/{id}")
    public JsonResult deleteTProductHandover(@PathVariable String id){
        itProductHandoverService.removeById(id);
        return new JsonResult(1,"ok",null);
    }

    /**
     * 查询用户
     * @return
     */
    @GetMapping("/selectUser")
    public JsonResult selectUser(){
        //获取用户信息
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        SUser suser = userMapper.selectOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
        //获取公司信息
        SCompany sCompany = isCompanyService.getById(suser.getCompanyId());
        return new JsonResult(1,sCompany.getCompanyName(),suser);
    }

    @GetMapping("/PrintHandoverForm/{s}")
    public JsonResult PrintHandoverForm(@PathVariable String s){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM HH:mm:ss");
        time=formatter.format(calendar.getTime());
        System.out.println(formatter.format(calendar.getTime()));
        String[] arr = s.split("-");
        for (int i = 0; i < arr.length; i++) {
            TProductHandover byId = itProductHandoverService.getItProductHandover(arr[i]);
            if (byId!=null){
                tProductHandovers.add(byId);
            }
        }

        return new JsonResult(1,"ok",null);
    }

    @GetMapping("/PrintHandover")
    public JsonResult PrintHandover(){
        int size = tProductHandovers.size();
        List<TProductHandover> t = new ArrayList<>();
        for (TProductHandover tProductHandover : this.tProductHandovers) {
            t.add(tProductHandover);
        }
        this.tProductHandovers.clear();
        return new JsonResult(size,time, t);
    }


}
